/**
 * 
 */
package research.sbookmarking.input;

// import org.connotea.*;
import research.sbookmarking.*;
import research.sbookmarking.input.xml.*;

import java.io.*;
import java.util.*;
import java.util.zip.*;

/**
 * This class controller for getting input, where it the commands to execute
 * against Connotea.org or retrieving the data from Connotea.org
 * <p>
 * 
 * @author <A HREF="mailto:aatleung@gmail.com">Albert Leung</a>
 * @version 1.0
 */
public class InputController {

	private static InputController anInstance = null;

	/**
	 * The only way to instantiate this class is through
	 * {@link #getInstance() getInstance}
	 */
	private InputController() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * Ensures that there is only one InputController instance. If an instance
	 * already exists it return the address of that instance otherwise it will
	 * instantiate an instance and save it.
	 * <p>
	 * 
	 * @return an instance of this object.
	 */

	public static InputController getInstance() {
		if (anInstance == null) {
			anInstance = new InputController();
		}

		return anInstance;
	}

	/**
	 * This method calls the appropriate method to extract the commands to be
	 * executed against Connotea from the given filename.
	 * <p>
	 * 
	 * @param filename
	 *            contains the name of the file that contains the commands to be
	 *            executed against Connotea.
	 * @return a list of {@link DataContainer DataContainer} where each
	 *         container contains the results from querying of Connotea.
	 */
	public List getQuery(String filename) {
		XmlController controller = new XmlController(filename);
		ArrayList results = new ArrayList();
		Iterator commands = controller.getCommands().iterator();

		while (commands.hasNext()) {
			Command command = (Command) commands.next();
			command.execute();
			results.add(command.getResult());
		}

		return results;
	}

	/**
	 * The method executes the commands that was retrieved from the method
	 * {@link #getCommands(String) getCommands}
	 * <p>
	 * 
	 * @param commands
	 *            a list of commands to be executed.
	 * @return the data in the {@link DataContainer DataContainer} object.
	 */
//	private DataContainer getData(ArrayList commands) {
//		Iterator items = commands.iterator();
//
//		while (items.hasNext()) {
//			Command aComment = (Command) items.next();
//			aComment.execute();
//		}
//
//		return packageData(commands);
//	}

	/**
	 * It extracts the data collected and combines it into a DataContainer
	 * object.
	 * <p>
	 * 
	 * @param aList
	 *            ArrayListdata
	 * @return the data in the {@link DataContainer DataContainer} object.
	 */
//	private DataContainer packageData(ArrayList aList) {
//		return null;
//	}
	private BufferedReader getFileStream (String filename) {
		try {
		FileInputStream fos = 
			new FileInputStream(filename);
		GZIPInputStream gzip = new GZIPInputStream (fos);
		InputStreamReader in = new InputStreamReader (gzip,"UTF-8");
		BufferedReader file = new BufferedReader (in);
		
		return file;
		}
		catch (IOException e){
			throw new RuntimeException(e);
		}
	}

	public List retrieveList (String filename){
		return null;
	}
	
	public List retrievedSavedData(List list, String filename) {
		return null;
	}

/*
	public List readQueryList (String filename) {
		try {
			ArrayList queryList = new ArrayList();
			FileInputStream fos = 
				new FileInputStream(filename);
			InputStreamReader in = new InputStreamReader (fos,"UTF-8");
			BufferedReader file = new BufferedReader (in);
			
			String temp;
			while ((temp = file.readLine())!=null){
				queryList.add(temp);
			}
		}
		catch (IOException e){
			throw new RuntimeException(e);
		}
		
	}
*/
	
	public static void main(String[] args) {
		// System.out.println (((new Object()).getClass()).getSimpleName());
		// System.out.println ("output: "
		// +InputController.getInstance().getQuery("setting.xml"));
		// System.out.println("size: "
		//]		+ InputController.getInstance().getQuery("setting.xml").size());
	}

}
